$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/user/manager',
        datatype: "json",
        colModel: [
            {label: '用户id', name: 'userId', index: "user_id", width: 30, key: true},
            {label: '管理员账号', name: 'userName', index: "user_name", width: 30},
            {label: '昵称', name: 'name', width: 15},
            {label: '门店id', name: 'storeId', index: "store_id", width: 40},
            {label: '手机号', name: 'mobile', index: "mobile", width: 40},
            {
                label: '角色',
                name: 'roleId',
                index: "role_id",
                width: 20,
                formatter: function (value, options, row) {
                    if (value == 2) {
                        return '<span class="label label-danger">商家</span>';
                    } else if (value == 3) {
                        return '<span class="label label-info">高级管理员</span>';
                    } else if (value == 4) {
                        return '<span class="label label-success">普通管理员</span>';
                    } else {
                        return "";
                    }
                }
            },
            {label: '添加人', name: 'insertByUserName', index: "insert_by_user_name", width: 40},
            {label: '创建时间', name: 'insertDate', index: "insert_date", width: 40},
        ],
        viewrecords: true,
        height: 720,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
    });

});

var vm = new Vue({
    el: '#dcapp',
    data: {
        q: {
            name: null,
        },
        id: null,
        showList: true,
        title: null,
        user: {},
        auth: 3,
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.user = {};
            vm.showList = false;
            vm.title = "添加管理员";
        },
        save: function () {
            this.user.auth = this.auth;
            var url = "sys/user/createM";
            var formJson = JSON.stringify(vm.user);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要解绑选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/user/freeManager",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        captcha1: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "common/sendCode/" + vm.user.username + "/0",
                success: function (r) {
                    if (r.code != 0) {
                        alert(r.msg);
                    }
                }
            });
        },
        captcha2: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "common/merchantCode",
                success: function (r) {
                    if (r.code != 0) {
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
    }
});